using System;
namespace Pay.Core {
	public static class DateTimeExtension {
		/// <summary>
		/// unix时间转为datetime
		/// </summary>
		/// <returns>The date time.</returns>
		/// <param name="timestamp">Timestamp.</param>
		public static DateTime ToDateTime(this long timestamp) {
			DateTime time = DateTime.MinValue;
			DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
			time = startTime.AddMilliseconds(timestamp);
			return time;
		}
		/// <summary>
		/// unix时间转为datetime
		/// </summary>
		/// <returns>The date time.</returns>
		/// <param name="timestamp">Timestamp.</param>
		public static DateTime? ToDateTime(this long? timestamp) {
			if (timestamp.HasValue) return ToDateTime(timestamp.Value);
			return null;
		}
		/// <summary>
		/// datetime转为unix时间
		/// </summary>
		/// <returns>The long.</returns>
		/// <param name="time">Time.</param>
		public static long ToUnix(this DateTime time) {
			DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
			long t = (time.Ticks - startTime.Ticks) / 10000;
			//除10000调整为13位
			return t;
		}
		/// <summary>
		/// datetime转为unix时间
		/// </summary>
		/// <returns>The long.</returns>
		/// <param name="time">Time.</param>
		public static long? ToUnix(this DateTime? time) {
			if (time.HasValue) return ToUnix(time.Value);
			return null;
		}
		public static DateTime ToDateTime(this int timestamp) {
			DateTime time = DateTime.MinValue;
			DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
			time = startTime.AddSeconds(timestamp);
			return time;
		}
		public static int ToUnixInt(this DateTime time) {
			System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
			return (int)(time - startTime).TotalSeconds;
		}

		public static DateTime ToDateTime(this string dt_str) {
			return DateTime.ParseExact(dt_str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
		}
	}
}